package top.kuanghua.email.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.kuanghua.email.service.SendEmailService;
import top.kuanghua.uploadapi.feign.UploadFeign;

import javax.mail.MessagingException;
import java.io.IOException;

/**
 * @Title: SendEmailController
 * @Description:
 * @Auther: kuanghua
 * @create 2020/8/20 22:41
 */
@Api(tags = "发送邮件")
@RestController
@RequestMapping("email")
public class SendEmailController {

    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private UploadFeign uploadFeign;

    @PostMapping("uploadFeignTest")
    public String uploadFeignTest() {
        System.out.println("好烦啊mail");
        String wolaile = uploadFeign.FeignTest("真的好伐啊");
        return wolaile;
    }

    @ApiOperation(value = "上传普通文本邮件")
    @GetMapping("sendEmailText")
    public String sendEmailText(String subject, String text, String sendForm, String sendTo) {
        sendEmailService.sendEmailText(subject, text, sendForm, sendTo);
        return "success";
    }

    @ApiOperation(value = "上传普通文本邮件", notes = "可以带附件 ")
    @PostMapping("sendMimeMail")
    public String sendMimeMail(String subject, String text, String sendForm, String sendTo, MultipartFile file)
            throws IOException, MessagingException, InterruptedException {

        if (file.getOriginalFilename().isEmpty()) {
            return "文件上传空";
        }

        sendEmailService.sendMimeMail(subject, text, sendForm, sendTo, file);
        return "success";
    }
}
